<?php
    error_reporting(E_ALL || ~E_DEPRECATED || ~E_STRICT || ~E_NOTICE || ~E_WARNING || ~E_CORE_WARNING);
    if($_FILES["filename"]["error"] > 0)
    {
        $echostr = "Error: " . $_FILES["filename"]["error"] . "<br />";
		file_put_contents("test.log", "$echostr\n", FILE_APPEND);
//		echo "<script language=\"JavaScript\">alert('$echostr');</script><br/>";
        echo "<script language=\"JavaScript\">self.setTimeout(window.location.href='/door/serversetting.php',5000);</script><br/>";	
		return;
    }else if (file_exists("upload/".$_FILES["filename"]["name"]))
    {
        $echostr = $_FILES["filename"]["name"] . " 已经存在. ";
		file_put_contents("test.log", "$echostr\n", FILE_APPEND);
	    echo "<script language=\"JavaScript\">alert('$echostr');</script><br/>";
		echo "<script language=\"JavaScript\">self.setTimeout(window.location.href='/door/serversetting.php',500);</script><br/>";	
		return;
    }
    else
    {        
		$fname=basename($_FILES["filename"]["name"]);
		move_uploaded_file($_FILES["filename"]["tmp_name"],"upload/".$fname);
        $echostr = "提交成功,开始下发。文件保存在：" . "upload/".$fname;
		echo "<script language=\"JavaScript\">alert('$echostr');</script><br/>";
		echo "<script language=\"JavaScript\">self.setTimeout(window.location.href='/door/serversetting.php',5000);</script><br/>";			
    }
	$myconn=@mysql_connect("localhost","root","root");
    if(!$myconn)
    {
        $string = "Can't connect database : ".mysql_error();
        file_put_contents("test.log", $string, FILE_APPEND);
	    die('Could not connect: '.mysql_error());
//		echo "fail";
		return;
    }
    $db_selected=mysql_select_db("dwaccessDB",$myconn);
	if(!$db_selected)
    {        
	    die("Can't use database dwaccessDB : ".mysql_error());
        $string = "Can't use database : ".mysql_error();
        file_put_contents("test.log", $string, FILE_APPEND);
		mysql_close($myconn);
//		echo "fail";
		return;
	}
	$exec="SELECT serverIP,serverPORT FROM serversetting;"; 
	$result=mysql_query($exec); 
	if((mysql_affected_rows()==0) or (mysql_affected_rows==-1)) 
	{ 
		mysql_free_result($result);
		mysql_close($myconn);
		$string = "Can't exec database : ".mysql_error();
        file_put_contents("test.log", $string, FILE_APPEND);
//		echo "fail";
		exit; 
	}
    $item = mysql_fetch_row($result);
    $transid=uuid();
//	$data=file_get_contents('php://input');
//	echo "<script language=\"JavaScript\">alert('1...'+'$data');</script><br/>";

	if($_POST['controlernum'])
    {
        $controlernum = $_POST['controlernum'];
//		echo "<script language=\"JavaScript\">alert('3...'+'$controlernum');</script><br/>";
    }
//    else
//    {
//        if($_GET['controlernum'])
//        {
//        	$action = $_GET['controlernum'];
//				echo "<script language=\"JavaScript\">alert('4...'+'$action');</script><br/>";
//        }  	
//    }
//	echo "<script language=\"JavaScript\">alert('2...'+'$controlernum');</script><br/>";	
	$cmdstr="{\"trans_id\":\"".$transid."\",\"cmd_code\":\"DW_APPUPGRADE\",\"url\":\"http://$item[0]:$item[1]/dwDownLoadfile.php?key=$fname\"}";				
//	@file_put_contents("test.log", "\n cmdstr =".$cmdstr."\n", FILE_APPEND);
//	$exec="SELECT controlerNum FROM gatesetting;";
//    $result=mysql_query($exec);    
//    while($item = mysql_fetch_row($result))
//	{
    $strSql="INSERT INTO cmdform(controlerNum,transid,cmdstr) VALUES('".$controlernum."','".$transid."','".$cmdstr."');";
    $result=mysql_query($strSql,$myconn);
	mysql_free_result($result);
//	@file_put_contents("test.log", "\n strSql =".$strSql."\n", FILE_APPEND);
	mysql_close($myconn);
//     echo "success";
//	}
    function uuid() 
	{
        if (function_exists('com_create_guid')) 
		{
            return com_create_guid();
        }else 
		{
            mt_srand((double)microtime()*10000);                   //optional for php 4.2.0 and up.
            $charid = md5(uniqid(rand(),true)); 
            return $charid;
        }
    }
    exit(0);
?>